package com.example.tys.baseapplication.View.recycleView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.tys.baseapplication.util.DisplayUtil;

/**
 * drawGridView
 *
 * @author Tys
 * @date 2017/3/20
 * @see {@link DividerItemDecoration}
 */
public class DividerGridItem extends RecyclerView.ItemDecoration {

    private int mDividerWidth = (int) DisplayUtil.dp2px(3);
    private Drawable mDivider;
    private Paint mPaint;
    private final Rect mBounds = new Rect();
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public DividerGridItem(Context context, int drawableRid) {
        initPaint();
        mDivider = context.getResources().getDrawable(drawableRid);
    }

    public DividerGridItem(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(mDividerWidth);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        RecyclerView.LayoutManager layoutManger = parent.getLayoutManager();
        if (layoutManger != null) {
            if (layoutManger instanceof GridLayoutManager) {
                outRect.set(mDividerWidth / 2, mDividerWidth / 2, mDividerWidth / 2, mDividerWidth / 2);
            } else {
                throw new IllegalArgumentException("oly support GridLayoutManager");
            }
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        RecyclerView.LayoutManager layoutManger = parent.getLayoutManager();
        if (layoutManger != null) {
            if (layoutManger instanceof GridLayoutManager) {
                drawGridView(c, parent);
            } else {
                throw new IllegalArgumentException("oly support GridLayoutManager");
            }
        }
    }


    private void drawGridView(Canvas canvas, RecyclerView parent) {
        drawGridHorizontalDivider(canvas, parent);
        drawGridVerticalDivider(canvas, parent);
    }

    private void drawGridVerticalDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = linearLayoutManager.getSpanCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;

            int left = 0;
            int right = 0;

            //左边第一列
            if ((i % spanCount) == 0) {
                //item左边分割线
                left = child.getLeft();
                right = left + mDividerWidth;
                if (null != mDivider) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                } else {
                    c.drawRect(left, top, right, bottom, mPaint);
                }
                //item右边分割线
                left = child.getRight() + params.rightMargin - mDividerWidth;
                right = left + mDividerWidth;
            } else {
                //非左边第一列
                left = child.getRight() + params.rightMargin - mDividerWidth;
                right = left + mDividerWidth;
            }
            //画分割线
            if (null != mDivider) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            } else {
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    private void drawGridHorizontalDivider(Canvas c, RecyclerView parent) {

        final int childCount = parent.getChildCount();
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = linearLayoutManager.getSpanCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            final int left = child.getLeft() - params.leftMargin - mDividerWidth;
            final int right = child.getRight() + params.rightMargin;
            int top = 0;
            int bottom = 0;

            // 最上面一行
            if ((i / spanCount) == 0) {
                //当前item最上面的分割线
                top = child.getTop();
                //当前item下面的分割线
                bottom = top + mDividerWidth;
                if (null != mDivider) {
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                } else {
                    c.drawRect(left, top, right, bottom, mPaint);
                }
                top = child.getBottom() + params.bottomMargin;
                bottom = top + mDividerWidth;
            } else {
                top = child.getBottom() + params.bottomMargin;
                bottom = top + mDividerWidth;
            }
            //画分割线
            if (null != mDivider) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            } else {
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    public DividerGridItem setDrawable(@NonNull Drawable drawable) {
        mDivider = drawable;
        return this;
    }
}


